import java.util.*;

/**
 * @author LKQ
 * @date 2022/5/21 14:50
 * @description 滑动窗口
 */
public class Solution3 {
    public static void main(String[] args) {
        Solution3 solution3 = new Solution3();
        int[] n = {1, 1, 1};
        solution3.longestSubarray(n);
    }
    public int longestSubarray(int[] nums) {
        int ans = 0, n = nums.length, cnt = 0;
        for (int i = 0, j = 0; i < n; i++) {
            if (nums[i] == 0) {
                cnt++;
            }
            // 维持窗口内至多一个0
            while (cnt > 1) {
                if (nums[j++] == 0) {
                    cnt--;
                }
            }
            ans = Math.max(ans, i - j);
        }
        return ans;
    }
}
